package com.easy.auth.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.easy.auth.mapper.UserMapper;
import com.easy.auth.util.PasswordDecode;
import com.easy.auth.util.TokenUtil;
import com.easy.common.bean.User;
import com.easy.common.exception.ServiceException;
import com.easy.common.service.IUserService;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.stereotype.Service;

import java.util.HashMap;

@Service
@DubboService
public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements IUserService {

    @Override
    public String login(String username, String password) {
        QueryWrapper<User> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda().eq(User::getUsername, username);
        User user = this.getOne(queryWrapper);
        if(user==null){
            throw new ServiceException("账号不存在");
        }
        //验证密码
        if(PasswordDecode.verify(password,user.getPassword())){
            HashMap<String,Object> map = new HashMap<>();
            map.put(TokenUtil.TYPE,TokenUtil.TYPE_USER);
            map.put(TokenUtil.USERID,user.getId());
            return TokenUtil.generateToken(map);
        }
        throw new ServiceException("密码错误");
    }
}
